package com.skzh.web.controller.pilotlicense;

import java.util.List;

import com.skzh.common.config.SkzhConfig;
import com.skzh.common.utils.file.MimeTypeUtils;
import com.skzh.pilotlicense.service.IPilotLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.skzh.common.annotation.Log;
import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.core.domain.model.LoginUser;
import com.skzh.common.enums.BusinessType;
import com.skzh.common.utils.file.FileUploadUtils;
import com.skzh.pilotlicense.domain.PilotLicense;

/**
 * 飞行执照信息
 *
 * @author skzh
 */
@RestController
@RequestMapping("/system/user/profile/license")
public class SysPilotLicenseController extends BaseController {


    @Autowired
    private IPilotLicenseService pilotLicenseService;

    /**
     * 获取飞行执照列表
     */
    @GetMapping("/list")
    public AjaxResult list() {
        LoginUser loginUser = getLoginUser();
        List<PilotLicense> list = pilotLicenseService.selectPilotLicenseList(loginUser.getUserId());
        return success(list);
    }

    /**
     * 新增飞行执照
     */
    @Log(title = "飞行执照", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PilotLicense pilotLicense) {
        pilotLicense.setUserId(getUserId());
        return toAjax(pilotLicenseService.insertPilotLicense(pilotLicense));
    }

    /**
     * 修改飞行执照
     */
    @Log(title = "飞行执照", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PilotLicense pilotLicense) {
        pilotLicense.setUserId(getUserId());
        return toAjax(pilotLicenseService.updatePilotLicense(pilotLicense));
    }

    /**
     * 删除飞行执照
     */
    @Log(title = "飞行执照", businessType = BusinessType.DELETE)
    @DeleteMapping("/{licenseIds}")
    public AjaxResult remove(@PathVariable Long[] licenseIds) {
        return toAjax(pilotLicenseService.deletePilotLicenseByIds(licenseIds));
    }

    /**
     * 上传执照图片
     */
    @Log(title = "执照图片", businessType = BusinessType.UPDATE)
    @PostMapping("/image")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String imagePath = FileUploadUtils.upload(SkzhConfig.getAvatarPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imagePath", imagePath);
            return ajax;
        }
        return error("请选择要上传的图片");
    }


}
